package com.example.mapper;

import com.example.bean.Collect;
import com.example.bean.Goods;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CollectMapper {

    /**
     * 商家查询当个商品被收藏的次数
     *
     * @param id 商品id
     * @return
     */
    @Select("select count(*) from collect where g_id=#{id}")
    public int collectNumber(int id);

    //查询用户收藏
    @Select("SELECT goods.g_id,goods.m_id,goods.g_name,goods.price,goods.g_describe,goods.g_inventory,goods.`g_img` "+
            "FROM collect RIGHT JOIN goods ON (collect.g_id = goods.g_id) where u_id=#{uid}")
    public List<Goods> SelectGoodsByUser(int uid);

    /**
     * 点击收藏收藏商品
     *
     * @param uid
     * @param gid
     * @return
     */
    @Insert("insert into collect(u_id,g_id) values(#{uid},#{gid})")
    int ins(int uid, int gid);

    /**
     * 删除收藏的商品
     *
     * @param uid
     * @param gid
     * @return
     */
    @Delete("delete from collect where u_id=#{uid} and g_id=#{gid}")
    int deleteCollect(int uid, int gid);

    /**
     * 根据用户id和商品id查询用户是否收藏过该商品
     *
     * @param uid
     * @param gid
     * @return
     */
    @Select("select * from collect where u_id=#{uid} and g_id=#{gid}")
    Collect selectById(int uid, int gid);
}
